package Day2025_04_07;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        while (T-- != 0) {
            int n = in.nextInt(), k = in.nextInt();
            in.nextLine();
            String s = in.nextLine();
            long ret = winStart (k, s.toCharArray());
            System.out.println(ret);
        }
    }
    private static long winStart (int k, char[] s) {
        int n = s.length;
        long tmp = 0, ret = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == 'W') {
                tmp++;
                if (tmp >= 3) ret += k;
                else ret += 1;
            } else {
                ret--;
                tmp = 0;
            }
        }
        return ret;
    }
}
